/*
 * @Author: Hp_lucky 1063512385@qq.com
 * @Date: 2024-09-04 17:52:20
 * @LastEditors: Hp_lucky 1063512385@qq.com
 * @LastEditTime: 2024-09-24 14:44:05
 * @FilePath: \cocos_frame\assets\script\src\Basic\Manager\KeyBoardManager.ts
 */
import { _decorator, Component, EventKeyboard, input, Input, KeyCode } from 'cc';
const { ccclass } = _decorator;

@ccclass('KeyBoardManager')
export class KeyBoardManager extends Component {
  static Sigleton: KeyBoardManager;

  constructor() {
    super();
    KeyBoardManager.Sigleton = this;
  }

  protected start(): void {
    //debug
    input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
  }

  /** 按住的按键 */
  private downKeys: Set<KeyCode> = new Set();
  /** 键盘事件 */
  onKeyDown(event: EventKeyboard) {
    this.downKeys.add(event.keyCode);
    // TEST.testKey(event.keyCode);
  }

  onKeyUp(event: EventKeyboard) {
    if (this.downKeys.has(event.keyCode)) {
      this.downKeys.delete(event.keyCode);
    }
  }

  getDownsKey(): Set<KeyCode> {
    return this.downKeys;
  }

  get isCtrlDown(): boolean {
    return this.downKeys.has(KeyCode.CTRL_LEFT) || this.downKeys.has(KeyCode.CTRL_RIGHT);
  }

  get isShiftDown(): boolean {
    return this.downKeys.has(KeyCode.SHIFT_LEFT) || this.downKeys.has(KeyCode.SHIFT_RIGHT);
  }

  get isAltDown(): boolean {
    return this.downKeys.has(KeyCode.ALT_LEFT) || this.downKeys.has(KeyCode.ALT_RIGHT);
  }
}
